<template>
    <div class="VersionCheck_div">
        <div class="components_title">
            <TitleTop>

            </TitleTop>

            <TitleMenu>
                <div slot="titleMenu_ico" @click="$router.go(-1)">
                    <div class="title_left_ico">
                        <a-icon type="left" class="Group_ico" theme="outlined" />
                    </div>
                    <div class="title_left_text">返回</div>
                </div>
                <div slot="titleMenu_text" class="Homework_titleMenu_text">
                    <span>版本检查</span>
                </div>
                <div slot="titleMenu_add">
                    <a-icon type="sync" style="font-size: 7vw;" theme="outlined" />
                </div>
            </TitleMenu>
        </div>
        <div id="VersionCheck_content" class="content_104class">
            <a-row class="VersionCheck_row">
                <a-col :span="24" class="VersionCheck_col_left" style="font-size: 5.5vw;color: gray;" >
                    {{showVersion}}
                </a-col>
                <a-col :span="24" class="VersionCheck_col_left" >
                    {{showText}}
                </a-col>

            </a-row>

        </div>

        <div class="showBox" v-if="Loading" >
            <div class="loading"></div>
            <div class="progress_div" style="color: deepskyblue">最新版本更新中......</div>
        </div>

        <FooterChaanel></FooterChaanel>
    </div>
</template>
<style>
    #VersionCheck_content{

    }
    .VersionCheck_row{
        width: 100vw;
        height: 12vw;
        margin: 8vw 0 3vw 0;
    }
    .VersionCheck_col_left{
        height: 12vw;
        line-height: 12vw;
        font-size: 6.5vw;
        generic-family: "Microsoft YaHei";
        text-align: center;
    }


    .VersionCheck_col_right .ant-select{
        width: 60vw;
        height: 12vw;
        line-height: 12vw;
    }
    .ant-select-dropdown{
        height: 100vw;
        overflow: scroll;
    }
    .el-loading-spinner{
        height: 20vw;
    }
    .el-icon-loading{
        font-size: 14vw;
    }
    .el-loading-spinner .el-loading-text{
        font-size: 6vw;
    }
</style>
<script>
    import TitleTop from './../components/TitleTop'
    import TitleMenu from  './../components/TitleMenu'
    import ARow from "ant-design-vue/es/grid/Row";
    import ACol from "ant-design-vue/es/grid/Col";
    import FooterChaanel from "../components/FooterChaanel";
    import axios from  'axios'
    export default {
        name: "GroupAdd",
        components:{FooterChaanel, ACol, ARow, TitleTop,TitleMenu},
        data() {
            return {
                msg: 'hello vue',
                showText:'',
                showVersion:'',
                Loading: false,
                checkVersions:{},
            }
        },
        mounted(){
            this.checkVersion();
        },
        methods:{
            checkVersion(){
                let me = this;
                me.$api.getVersion().then( resp => {
                    //状态码200
                    if(resp.status === 200){
                        me.checkVersions = resp.data.result;
                        console.log(me.checkVersions);
                        let myVersionString = localStorage.getItem("gis4_appVersion");//当前安装app的版本号
                        let myVersion = parseInt(myVersionString);//当前安装app的版本号
                        console.log("myVersion",myVersion);
                        let sortVesion = Array.sort(me.checkVersions);//将其排序
                        console.log("sortVesion",sortVesion);
                        let lastVersionStringPoint = sortVesion[(sortVesion.length-1)].version;//得到最后最新的版本号String类型,如1.07.16.1020
                        let lastVersionStringArray =  lastVersionStringPoint.split(".");//如：["1", "07", "16", "1020"]
                        let lastVersionStringInt = lastVersionStringArray.join("");//如:"107161020"
                        let lastVersion = parseInt(lastVersionStringInt);//得到最后最新的版本号Int类型,如107161020
                        console.log("当前版本:" + myVersion + ",系统版本:" + lastVersion);

                        if(lastVersion === myVersion){
                            me.showVersion = "系统最新版本:"+lastVersionStringPoint;
                            me.showText = "您已经是最新版本，无需更新。"

                        }else{
                            me.showVersion = "系统最新版本 "+lastVersionStringPoint;
                            me.showText = "发现新版本，是否更新？";
                            //下方可以出现一个更新按钮
                            let message = "最新版本:V"+lastVersionStringPoint+" 请安装新版本后使用,如遇到安装未知软件来源，请设置允许即可";
                            let title = "版本更新";
                            let buttonLabels = ["确认",""];//出现界面：2取消 1确认

                            if(typeof(navigator)==='undefined' || typeof(navigator.notification)==='undefined' || typeof(navigator.notification.confirm)==='undefined') {
                                console.log(message);
                                return;
                            }
                            navigator.notification.confirm(message, confirmCallback, title, buttonLabels);
                            function confirmCallback(buttonIndex) {
                                console.log(buttonIndex);
                                if(buttonIndex===0){
                                    me.checkVersion();
                                }else{
                                    me.Loading=true;
                                    me.update(lastVersionStringPoint);
                                }
                            }
                        }
                    }
                }).catch( error =>{
                    //请求失败
                    console.log("init version failed:"+error);

                })
            },
            update(lastVersionStringPoint){
                let me = this;
                console.log("更新下载中......");
                let fileTransfer = new window.FileTransfer();
                let joinUrl = "http://files.gis4.tdzcsoft.com/files/apk/" + lastVersionStringPoint+ ".apk";//与服务器app文件名和接口列表版本一致

                let uri = encodeURI(joinUrl);//'http://files.gis4.tdzcsoft.com/files/apk/patrol_V1.05.21.apk'
                //let fileURL = 'cdvfile://localhost/temporary/patrol_V1.05.21.apk';
                let fileURL = 'cdvfile://localhost/persistent/patrol/file/v'+ lastVersionStringPoint + '.apk';

                fileTransfer.download(
                    uri, fileURL, function (entry) {
                        me.Loading = false;
                        console.log('更新下载完成: ');
                        console.log(entry.toURL());
                        cordova.plugins.fileOpener2.open(
                            fileURL,
                            'application/vnd.android.package-archive'
                        );

                    },
                    function (error) {
                        console.log('下载更新版本出错:');
                        console.log(error);
                        console.log(error.source );
                        console.log(error.target );
                        console.log(error.code );
                        me.Loading = false;
                    },
                    false, {
                        headers: {
                            'Authorization': 'Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=='
                        }
                    }
                )

            },


        },

    }
</script>

